package com.syt.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syt.commonutils.JwtUtils;
import com.syt.commonutils.MD5;
import com.syt.educenter.entity.vo.ResisterVo;
import com.syt.educenter.mapper.UcenterMemberMapper;
import com.syt.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syt.educenter.entity.UcenterMember;
import com.syt.servicebase.exceptionHandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author SYT
 * @since 2021-03-10
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录的方法
    @Override
    public String login(@RequestBody UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"登录失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember == null){ //没有手机号
            throw new MyException(20001,"手机号不存在");
        }
        //判断密码
        //数据库密码经过加密
        //把输入的密码进行加密,再和数据库密码进行比较
        //加密方式MD5
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new MyException(20001,"密码错误");
        }
        if (mobileMember.getIsDisabled()){
            throw new MyException(20001,"用户已被禁用");
        }
        //登录成功
        //生成token字符串,使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    //注册方法
    @Override
    public void register(ResisterVo resisterVo) {
        //获取注册的数据
        String code = resisterVo.getCode(); //验证码
        String mobile = resisterVo.getMobile();//手机号
        String nickname = resisterVo.getNickname();//昵称
        String password = resisterVo.getPassword();//密码
        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)){
            throw new MyException(20001,"登录失败");
        }
        //判断验证码
        //获取redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new MyException(20001,"验证码超时");
        }
        //判断手机号是同步
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new MyException(20001,"手机号重复");
        }
        //添加数据到数据库
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password)); //密码需要加密
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
    }

    //判断数据库中是否存在相同的微信信息
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
